package com.cskaoyan.day5.deque._04_blockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 消费者
 * 从阻塞队列里获取数据
 */
class Consumer implements Runnable {
    BlockingQueue<String> blockingQueue;

    public Consumer(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                String take = blockingQueue.take();
                System.out.println("消费者获取到了一份数据:" + take);

                // 可以通过它来调整消费的速度
                TimeUnit.MILLISECONDS.sleep(100);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
